package com.iflytek.speech.util;

import android.os.Handler;
import android.util.Log;
import com.example.health.ui.MyAppliction;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/*
 *  @项目名：  trunk
 *  @包名：    com.sunson.sst.util
 *  @文件名:   HttpUtils
 *  @创建者:   ${小陈}
 *  @创建时间:  2018/9/17 11:21
 *  @描述：    网络请求。
 */
public class HttpUtils {
   private static Handler mHandler;
    public static void  setHandler(Handler handler){
        mHandler = handler;
    }
    public static void post(String json, String url, final HttpUtilsCallBack httpUtilsCallBack) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);

        Request request = new Request.Builder()
                .url(url)//请求的url
                .post(requestBody)
                .build();

        //创建/Call
        Call call = okHttpClient.newCall(request);
        //加入队列 异步操作
        call.enqueue(new Callback() {
            //请求错误回调方法
            @Override
            public void onFailure(Call call, IOException e) {
                final String message = e.getMessage();
                Log.d("sss", "http:onFailure: " + message);
                MyAppliction.Companion.getHandler().post(new Runnable() {
                    @Override
                    public void run() {

                        if (httpUtilsCallBack != null) {
                            httpUtilsCallBack.error(message);
                        }
                    }
                });
            }
            @Override
            public void onResponse(final Call call, final Response response) throws IOException {
                final String string = response.body().string();
             //   Log.d("post", "run: " + string);
                MyAppliction.Companion.getHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        if (httpUtilsCallBack != null) {
                            if (response.isSuccessful()) {
                                httpUtilsCallBack.successed(string);
                            } else {
                                httpUtilsCallBack.error("请求失败");
                            }
                        }
                    }
                });
            }
        });
    }

    public static void get(String url, final HttpUtilsCallBack httpUtilsCallBack) {
        //创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();

        final Request request = new Request.Builder()
                .url(url)//请求的url
                .get()//设置请求方式，get()/post()  查看Builder()方法知，在构建时默认设置请求方式为GET
                .build(); //构建一个请求Request对象

        //创建/Call
        Call call = okHttpClient.newCall(request);
        //加入队列 异步操作
        call.enqueue(new Callback() {
            //请求错误回调方法
            @Override
            public void onFailure(Call call, final IOException e) {
                MyAppliction.Companion.getHandler().post(new Runnable() {
                    @Override
                    public void run() {

                        if (httpUtilsCallBack != null) {
                            httpUtilsCallBack.error(e.getMessage());
                        }
                    }
                });
            }

            //异步请求(非主线程)
            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if (response.code() == 200) {
                    MyAppliction.Companion.getHandler().post(new Runnable() {
                        @Override
                        public void run() {

                            if (httpUtilsCallBack != null) {
                                try {
                                    httpUtilsCallBack.successed(response.body().string());
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                }
            }
        });

    }

    public interface HttpUtilsCallBack {
        void error(String error);

        void successed(String string);
    }
}
